﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Smart.Common
{
    public partial class xucBaseAdd : xucBase
    {
        public event ButtonClickEventHander CancelClick;
        public event ButtonClickEventHander SaveClick;
        public xucBaseAdd()
        {
            InitializeComponent();
            this.Init();
        }
        public virtual void Add()
        {
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.RaiseCancelClickEventHander();
        }

        private void btnCancel_KeyDown(object sender, KeyEventArgs e)
        {
            this.btnCancel_Click(this.btnCancel, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            base.IsClose = CloseOrNew.Close;
            this.Save();
        }

        private void btnSave_KeyDown(object sender, KeyEventArgs e)
        {
            this.btnSave_Click(this.btnSave, e);
        }

        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            base.IsClose = CloseOrNew.New;
            this.txtID.Properties.ReadOnly = false;
            this.Save();
            base.Status = Actions.Add;
            this.txtID.Focus();
            this.Add();
        }

        public virtual void Change()
        {
        }

        protected virtual bool Check()
        {
            return true;
        }

        public virtual void Delete()
        {
            this.uc_Delete();
        }

        protected virtual void Init()
        {
            this.ReLoad();
        }
        protected virtual void MakerInterface()
        {
        }

        public bool Number(KeyPressEventArgs e)
        {
            if (((e.KeyChar == '\b') | (e.KeyChar == '.')) | (e.KeyChar == '-'))
            {
                return false;
            }
            return !char.IsNumber(e.KeyChar);
        }

        public void RaiseCancelClickEventHander()
        {
            if (this.CancelClick != null)
            {
                this.CancelClick(this);
            }
        }

        public void RaiseSaveClickEventHander()
        {
            if (this.SaveClick != null)
            {
                this.SaveClick(this);
            }
        }

        public virtual void ReLoad()
        {
        }

        public virtual void Save()
        {
            if ((this.txtID.ErrorText != string.Empty) | (this.txtID.Text == string.Empty))
            {
                XtraMessageBox.Show((this.txtID.ErrorText.Length == 0) ? "Dữ liệu của \x00f4 n\x00e0y kh\x00f4ng được bỏ trống." : this.txtID.ErrorText, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtID.Focus();
            }
            else if ((this.txtNAME.ErrorText != string.Empty) | (this.txtNAME.Text == string.Empty))
            {
                XtraMessageBox.Show((this.txtNAME.ErrorText.Length == 0) ? "Dữ liệu của \x00f4 n\x00e0y kh\x00f4ng được bỏ trống." : this.txtNAME.ErrorText, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtNAME.Focus();
            }
            else if (this.Check())
            {
                if (base.Status == Actions.Add)
                {
                    this.uc_Save();
                }
                else if (base.Status == Actions.Update)
                {
                    this.uc_Update();
                }
                else if (base.Status == Actions.Change)
                {
                    this.uc_Change();
                }
            }
        }

        public virtual void SetData(object item)
        {
        }

        public void SetData(string ID)
        {
            this.txtID.Text = ID;
            this.txtNAME.Focus();
            this.Add();
        }

        protected virtual void SetInterface()
        {
        }

        private void txtID_Leave(object sender, EventArgs e)
        {
            TextEdit control = (TextEdit)sender;
            if ((base.Status != Actions.None) && (control.Text == string.Empty))
            {
                this.Err.SetError(control, "Vui l\x00f2ng nhập th\x00f4ng tin." + this.Text);
            }
        }

        private void txtNAME_EditValueChanged(object sender, EventArgs e)
        {
            TextEdit control = (TextEdit)sender;
            if ((base.Status != Actions.None) && (control.ErrorText != string.Empty))
            {
                this.Err.SetError(control, string.Empty);
            }
        }

        private void txtNAME_Leave(object sender, EventArgs e)
        {
            TextEdit control = (TextEdit)sender;
            if (base.Status != Actions.None)
            {
                if (control.Text == string.Empty)
                {
                    this.Err.SetError(control, "Vui l\x00f2ng nhập th\x00f4ng tin." + this.Text);
                }
                if (string.IsNullOrEmpty(this.txtID.Text) && !string.IsNullOrEmpty(this.txtNAME.Text))
                {
                    this.txtID.Text = base.GenerateCode(this.txtNAME.Text);
                }
            }
        }

        protected virtual string uc_Change()
        {
            return string.Empty;
        }

        protected virtual string uc_Delete()
        {
            return string.Empty;
        }

        protected virtual string uc_Save()
        {
            return string.Empty;
        }

        protected virtual string uc_Update()
        {
            return string.Empty;
        }
    }
}
